package com.example.yqand4v1_1.service;

import com.example.yqand4v1_1.dao.TeamEntityMapper;
import com.example.yqand4v1_1.dao.VolunteerEntityMapper;
import com.example.yqand4v1_1.dao.entity.Team;
import com.example.yqand4v1_1.dao.entity.Volunteer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class TeamService {
    @Autowired
    private TeamEntityMapper teamEntityMapper;
    @Autowired
    private VolunteerEntityMapper volunteerEntityMapper;
    public ArrayList<Team> queryTeamsByAmount(int count,int amount)
    {
        return teamEntityMapper.queryTeamsByAmount(count,amount);
    }
    public int addTeam(Team team)
    {
        teamEntityMapper.addTeam(team);
        int teamId=team.getTeamId();
        int isParticipate=participateTeam(teamId,team.getLeaderId());
        return isParticipate;
    }
    public Team querySingleTeamById(int teamId)
    {
        return teamEntityMapper.querySingleTeamById(teamId);
    }
    public Team queryTeamOfVolunteer(int volunteerId)
    {
        Integer teamId=teamEntityMapper.queryTeamIdOfVolunteer(volunteerId);
        if(teamId==null)return null;
        return teamEntityMapper.querySingleTeamById(teamId);
    }
    public int participateTeam(int teamId,int volunteerId)
    {
        Team team=teamEntityMapper.querySingleTeamById(teamId);
        Integer peopleNum=team.getPeopleNum()+1;
        teamEntityMapper.updatePeopleNumOfTeam(peopleNum,teamId);
        return teamEntityMapper.participateTeam(volunteerId,teamId);
    }
    public Map<String,String> queryNameAndPhoneNumOfLeader(int leadId)
    {
        Map<String,String> result=new HashMap<>();
        Volunteer volunteer=new Volunteer();
        volunteer.setId(leadId);
        Volunteer temp=volunteerEntityMapper.selectById(volunteer);
        result.put("leaderName", temp.getName());
        result.put("leaderPhoneNum", temp.getPhoneNum());
        return result;
    }
    public int queryTotalNumOfTeams()
    {
        return teamEntityMapper.queryTotalNumOfTeams();
    }
}
